﻿Imports System.IO

Public Class Rech_Mowakilin

    Private Sub Frm_Rech_Clients_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Partager.frmMoule.Enabled = True
    End Sub

    Private Sub Frm_Rech_Clients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        comb_matorido_elbaht.SelectedIndex = 0
        'Partager.moi.remplir_Ref()
    End Sub

    Private Sub txt_rech_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_rech.TextChanged
        Try
            If txt_rech.Text <> "" Then
                List_tous.Items.Clear()
                Partager.list_recherch.Clear()
                If comb_matorido_elbaht.SelectedItem = "رقم البطاقة الوطنية" Then
                    For Each clt As Client In Partager.moi.List_client
                        If clt.Cin.Contains(txt_rech.Text) Then
                            For Each refe As Reference In Partager.moi.List_Reference
                                'For Each it As Integer In refe.idsClt
                                '    If it = clt.Id Then
                                '        Partager.list_recherch.Add(refe)
                                '        List_tous.Items.Add(New ListViewItem(New String() {clt.Id, refe.Ref, clt.Nom, clt.Prenom, clt.Nom_ent}))
                                '    End If
                                'Next
                            Next
                        End If
                    Next


                ElseIf comb_matorido_elbaht.SelectedItem = "الاسم العائلي" Then
                    For Each clt As Client In Partager.moi.List_client
                        If clt.Nom.Contains(txt_rech.Text) Then

                            For Each refe As Reference In Partager.moi.List_Reference

                                'For Each it As Integer In refe.idsClt
                                '    If it = clt.Id Then
                                '        Partager.list_recherch.Add(refe)
                                '        List_tous.Items.Add(New ListViewItem(New String() {clt.Id, refe.Ref, clt.Nom, clt.Prenom, clt.Nom_ent}))
                                '    End If
                                'Next

                            Next

                        End If
                    Next
                ElseIf comb_matorido_elbaht.SelectedItem = "الاسم الشخصي" Then
                    For Each clt As Client In Partager.moi.List_client
                        If clt.Prenom.Contains(txt_rech.Text) Then
                            For Each refe As Reference In Partager.moi.List_Reference
                                'For Each it As Integer In refe.idsClt
                                '    If it = clt.Id Then
                                '        Partager.list_recherch.Add(refe)
                                '        List_tous.Items.Add(New ListViewItem(New String() {clt.Id, refe.Ref, clt.Nom, clt.Prenom, clt.Nom_ent}))
                                '    End If
                                'Next
                            Next
                        End If
                    Next
                ElseIf comb_matorido_elbaht.SelectedItem = "اسم الشركة" Then



                    For Each clt As Client In Partager.moi.List_client
                        If clt.Nom_ent.Contains(txt_rech.Text) Then
                            For Each refe As Reference In Partager.moi.List_Reference
                                'For Each it As Integer In refe.idsClt
                                '    If it = clt.Id Then
                                '        Partager.list_recherch.Add(refe)
                                '        List_tous.Items.Add(New ListViewItem(New String() {clt.Id, refe.Ref, clt.Nom, clt.Prenom, clt.Nom_ent}))
                                '    End If
                                'Next

                            Next
                        End If
                    Next

                ElseIf comb_matorido_elbaht.SelectedItem = "اسم الممثل القانوني" Then



                    For Each clt As Client In Partager.moi.List_client
                        If clt.Representant_legal.Contains(txt_rech.Text) Then
                            For Each refe As Reference In Partager.moi.List_Reference
                                'For Each it As Integer In refe.idsClt
                                '    If it = clt.Id Then
                                '        Partager.list_recherch.Add(refe)
                                '        List_tous.Items.Add(New ListViewItem(New String() {clt.Id, refe.Ref, clt.Nom, clt.Prenom, clt.Nom_ent}))
                                '    End If
                                'Next

                            Next
                        End If
                    Next
                Else

                    For Each refe As Reference In Partager.moi.List_Reference
                        If IsNumeric(txt_rech.Text) Then
                            If refe.Ref = CInt(txt_rech.Text) Then
                                Partager.list_recherch.Add(refe)
                                'For Each it As Integer In refe.idsClt
                                '    For Each clt As Client In Partager.moi.List_client
                                '        If it = clt.Id Then
                                '            List_tous.Items.Add(New ListViewItem(New String() {clt.Id, refe.Ref, clt.Nom, clt.Prenom, clt.Nom_ent}))
                                '        End If
                                '    Next
                                'Next
                                Exit For
                            End If
                        End If
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If txt_rech.Text = "" Then
            List_tous.Items.Clear()
        End If
    End Sub

    Private Sub Btn_choisi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_choisi.Click
        Dim avo As Reference
        If List_tous.SelectedIndices.Count > 0 Then

            If comb_matorido_elbaht.Text = "المرجع" Then
                Dim i As Integer = 0
                If Partager.list_recherch.Count > 0 Then


                    avo = Partager.list_recherch(0)
                    For Each cmb As Control In Partager.frmMoule.Controls

                        If TypeOf cmb Is ComboBox Then
                            If cmb.Name = "Comb_nom" Then
                                For Each it As DictionaryEntry In CType(cmb, ComboBox).Items
                                    If it.Key = avo.Ref Then
                                        CType(cmb, ComboBox).SelectedIndex = i
                                    End If
                                    i += 1
                                Next
                                Exit For

                            End If
                        End If

                    Next

                End If
                Me.Close()
            Else
                Dim j As Integer = 0
                Dim str As String = ""
                Try
                    For Each i As Integer In List_tous.SelectedIndices
                        avo = Partager.list_recherch(i)
                        For Each cmb As Control In Partager.frmMoule.Controls

                            If TypeOf cmb Is ComboBox Then
                                If cmb.Name = "Comb_nom" Then
                                    For Each it As DictionaryEntry In CType(cmb, ComboBox).Items
                                        If it.Key = avo.Ref Then
                                            CType(cmb, ComboBox).SelectedIndex = j
                                        End If
                                        j += 1
                                    Next
                                    Exit For

                                End If
                            End If

                        Next
                    Next
                    Me.Close()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
        MsgBox("اختر موكل من القائمة", MsgBoxStyle.Information, "خطأ في البحث")
        End If
    End Sub


    Private Sub comb_matorido_elbaht_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comb_matorido_elbaht.SelectedIndexChanged
        txt_rech.Clear()
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged

    End Sub
End Class